package com.zxp.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;


public class MainActivity extends AppCompatActivity implements OnClickListener {

    public static int MODE = MODE_PRIVATE;
    public static final String PREFERENCE_NAME = "SaveSetting";
    private String username, password;
    Button btnLogin;
    EditText etAccount, etPassword;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);
        sharedPreferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        if (sharedPreferences.getString("username", username) != null
                && sharedPreferences.getString("password", password) != null) {
            etAccount.setText(sharedPreferences.getString("username", username));
            etPassword.setText(sharedPreferences.getString("password", password));
        }

        btnLogin.setOnClickListener(this);
    }
    @Override
    public void onClick (View v){
        username = etAccount.getText().toString();
        password = etPassword.getText().toString();
        editor.putString("username", username);//写入用户名
        editor.putString("password", password);//写入密码
        editor.apply();
        Toast.makeText(MainActivity.this, "用户信息保存成功", Toast.LENGTH_SHORT).show();
    }
}
